﻿using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using kysec.RAL.EFToMySql;
using System.Xml;
using System.Xml.Linq;

namespace kysec.BLL.Production
{
    /// <summary>
    /// 查询产品中的内容
    /// </summary>
    public class Search
    {
        /// <summary>
        /// 查询样品演示
        /// </summary>
        /// <param name="type">样品类型：0:最新产品 1:系列产品</param>
        /// <param name="isDemo">是否是样品</param>
        /// <param name="takeitem">每页显示的条数</param>
        /// <param name="page">显示的页数</param>
        /// <returns>返回查找到的信息结果</returns>
        public static List<DemoData> getDemoProducts(int type,bool isDemo, int takeitem, int page)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
               .RetrieveObject(string.Format("GetDemoProducts_{0}_{1}_{2}_{3}", type, page, takeitem,DateTime.Now.Millisecond.ToString()));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                try
                {
                    var list = (from c in ctx.tb_product
                                where c.c_i_type == type && c.c_b_isDelete == false && c.c_b_isDemo == isDemo
                                orderby c.c_dt_sendTime descending
                                select new
                                {
                                    id=c.c_uq_pId,
                                    title=c.c_vc_name,
                                    imgPath = c.c_x_imgPath,
                                    model = c.c_vc_pModel,
                                    type = c.c_i_type
                                })
                               .Skip(page * takeitem)

                               .Take(takeitem)

                               .ToList();

                    #region 将信息放入Cahce中
                    if (list.Any())
                    {

                        var packlist = list.Select(c => new DemoData()
                        {
                            pid=c.id,
                            imgPath= XElement.Parse(c.imgPath),
                            title=c.title,
                            model = c.model,
                            type = Convert.ToInt32(c.type)
                        }).ToList();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                            .AddObjectWithAbsoluteTime(string.Format("GetDemoProducts_{0}_{1}_{2}_{3}", type, page, takeitem, DateTime.Now.Millisecond.ToString())
                            , packlist,
                            600000
                            ,System.Web.Caching.CacheItemPriority.Normal);
                        return packlist;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询样品演示时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<DemoData>();
            }
            return (List<DemoData>)o;
        }

        /// <summary>
        /// 查询产品的型号，用于左边显示
        /// </summary>
        /// <param name="type">样品类型：0:最新产品 1:系列产品</param>
        /// <param name="takeitem">每页显示的条数</param>
        /// <param name="page">显示的页数</param>
        /// <returns>返回查找到的信息结果</returns>
        public static List<DemoData> getProductsLeft(int type, int takeitem, int page)
        {
            var o =kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
               .RetrieveObject(string.Format("GetProductsLeft_{0}_{1}_{2}", type, page, takeitem));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                try
                {
                    var list = (from c in ctx.tb_product
                                where c.c_i_type == type && c.c_b_isDelete == false
                                orderby c.c_dt_sendTime descending
                                select new
                                {
                                    id = c.c_uq_pId,
                                    title = c.c_vc_name,
                                    imgPath = c.c_x_imgPath,
                                    model = c.c_vc_pModel,
                                    type = c.c_i_type,
                                    features = c.c_ntx_pFeatures,
                                    function=c.c_vc_pFunction
                                })
                               .Skip(page * takeitem)

                               .Take(takeitem)

                               .ToList();

                    #region 将信息放入Cahce中
                    if (list.Any())
                    {

                        var packlist = list.Select(c => new DemoData()
                        {
                            pid = c.id,
                            imgPath = XElement.Parse(c.imgPath),
                            title = c.title,
                            model = c.model,
                            type = Convert.ToInt32(c.type),
                            features = c.features,
                            function = c.function
                        }).ToList();
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                                .AddObjectWithAbsoluteTime(string.Format("GetProductsLeft_{0}_{1}_{2}", type, page, takeitem)
                                , packlist,
                                600000
                                , System.Web.Caching.CacheItemPriority.Normal);
                        return packlist;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询样品演示时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<DemoData>();
            }
            return (List<DemoData>)o;
        }

        /// <summary>
        /// 查询某一类产品信息
        /// </summary>
        /// <param name="type">样品类型：0:最新产品 1:系列产品</param>
        /// <param name="takeitem">每页显示的条数</param>
        /// <param name="page">显示的页数</param>
        /// <returns>返回查找到的信息结果</returns>
        public static List<tb_product> getTypeProducts(int type, int takeitem, int page)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
               .RetrieveObject(string.Format("GetTypeProducts_{0}_{1}_{2}", type, page, takeitem));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                try
                {
                    var list = ctx.tb_product.Where(c =>c.c_b_isDelete == false && c.c_i_type == type).Skip(page * takeitem).Take(takeitem).ToList();
                    #region 将信息放入Cahce中
                    if (list.Any())
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                            .AddObjectWithAbsoluteTime(string.Format("GetTypeProducts_{0}_{1}_{2}", type, page, takeitem)
                            , list,
                            600000
                            , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询某一类商品时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<tb_product>();
            }
            return (List<tb_product>)o;
        }


        /// <summary>
        /// 查询具体的产品
        /// </summary>
        /// <param name="id">产品ID 加密了的</param>
        /// <returns>返回找到的结果</returns>
        public static List<tb_product> getProducts(string id)
        {
            var o =kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                          .RetrieveObject(string.Format("GetProducts_{0}",id));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                id = kysec.CR.Common.EncodeByBase64.Decode(id);
                try
                {
                    var list = ctx.tb_product.Where(c => c.c_uq_pId.Equals(id) && c.c_b_isDelete == false).ToList();

                    #region 将信息放入Cahce中
                    if (list.Any())
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                            .AddObjectWithAbsoluteTime(string.Format("GetProducts_{0}", id)
                            , list,
                            600000
                            , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询具体商品时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search),error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<tb_product>();
            }
            return (List<tb_product>)o;
        }

        /// <summary>
        /// 查询具体的产品用于样品演示
        /// </summary>
        /// <param name="id">产品ID 加密了的</param>
        /// <returns>返回找到的结果</returns>
        public static List<DemoDetail> getProductsForDemo(string id)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                          .RetrieveObject(string.Format("GetProductsForDemo_{0}", id));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                id = kysec.CR.Common.EncodeByBase64.Decode(id);
                try
                {
                    var list = (from c in ctx.tb_product
                                where c.c_uq_pId.Equals(id) && c.c_b_isDelete == false
                                orderby c.c_dt_sendTime descending
                                select new
                                {
                                    id = c.c_uq_pId,
                                    imgPath = c.c_x_imgPath,
                                }).ToList();

                    #region 将信息放入Cahce中
                    if (list.Any())
                    {

                        var packlist = list.Select(c => new DemoDetail()
                        {
                            pId = c.id,
                            imgPath = XElement.Parse(c.imgPath)
                        }).ToList();
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                                .AddObjectWithAbsoluteTime(string.Format("GetProductsForDemo_{0}", id)
                                , packlist,
                                600000
                                , System.Web.Caching.CacheItemPriority.Normal);
                        return packlist;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询具体商品时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<DemoDetail>();
            }
            return (List<DemoDetail>)o;
        }

        /// <summary>
        /// 查询最新的产品
        /// </summary>
        /// <param name="id">产品ID 加密了的</param>
        /// <returns>返回找到的结果</returns>
        public static List<tb_product> getNewsProducts()
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                          .RetrieveObject(string.Format("GetNewsProducts_{0}", DateTime.Now.Second));
            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();               
                try
                {
                    var list = (from c in ctx.tb_product where c.c_b_isDelete == false orderby c.c_dt_sendTime descending select c).Take(1).Skip(0).ToList();

                    #region 将信息放入Cahce中
                    if (list.Any())
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                            .AddObjectWithAbsoluteTime(string.Format("GetNewsProducts_{0}", DateTime.Now.Second)
                            , list,
                            600000
                            , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询具体商品时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                }
                    #endregion

                return new List<tb_product>();
            }
            return (List<tb_product>)o;
        }
    }

    /// <summary>
    /// 产品演示的属性设置
    /// </summary>
    public struct DemoData
    {
        public string pid;
        public string title;
        public string model;
        public int type;
        public XElement imgPath;
        public string features;
        public string function;
    }
    /// <summary>
    /// 查找产品用于左边的显示
    /// </summary>
    public struct pDataLeft {
        public string pId;
        public string model;
        public string title;
        public int type;
    }

    /// <summary>
    /// 查找具体的产品的信息用于样品演示
    /// </summary>
    public struct DemoDetail
    {
        public string pId;
        public XElement imgPath;
    }
}

